﻿package com.plter.controls.asmax
{
	import com.plter.events.AFormEvent;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Jaja as-max.cn
	 */
	public class ADialog extends AForm
	{
		private var theHandler:Function;
		
		public static const OK:String = "确定";
		public static const CANCEL:String = "取消";
		public static const YES:String = "是";
		public static const NO:String = "否";
		
		private var bg:Sprite = new Sprite;
		
		private var cancelBtn:AButton = new AButton(ADialog.CANCEL);
		private var okBtn:AButton = new AButton(ADialog.OK);
		private var yesBtn:AButton = new AButton(ADialog.YES);
		private var noBtn:AButton = new AButton(ADialog.NO);
		private var btnArr:Array;
		
		/**
		 * 装载按钮的面板
		 */
		private var btnPnal:Sprite = new Sprite;
		
		/**
		 * 创建一个对话框
		 * @param	mode		对话框模式MessageDialogMode中的常量
		 * @param	handler		对话框关闭时的处理器
		 */
		public function ADialog(mode:String = "ok", handler:Function = null ) :void
		{
			//绘制对话框背景层
			bg.graphics.beginFill(0xFFFFFF, 0.01);
			bg.graphics.drawRect(0, 0, 400, 300);
			bg.graphics.endFill();
			
			maxable = false;
			
			theHandler = handler;
			this.addEventListener(AFormEvent.AFORM_CLOSING, closingForm);
			this.addEventListener(Event.ADDED_TO_STAGE, addToStage);
			bg.addEventListener(MouseEvent.MOUSE_DOWN, clickBG);
			
			switch(mode) {
				case AMessageDialogMode.CANCEL:
					btnArr = [cancelBtn];
					break;
				case AMessageDialogMode.OK:
					btnArr = [okBtn];
					break;
				case AMessageDialogMode.OK_CANCEL:
					btnArr = [okBtn, cancelBtn];
					break;
				case AMessageDialogMode.YES:
					btnArr = [yesBtn];
					break;
				case AMessageDialogMode.YES_NO:
					btnArr = [yesBtn, noBtn];
					break;
				case AMessageDialogMode.YES_NO_CANCEL:
					btnArr = [yesBtn, noBtn, cancelBtn];
					break;
			}
			
			//为按钮添加侦听器
			for (var i:int = 0; i < btnArr.length; i++) {
				if (i == 0) {
					btnArr[i].x = 0;
				}else {
					btnArr[i].x = btnArr[i - 1].x + btnArr[i - 1].width + 10;
				}
				btnPnal.addChild(btnArr[i]);
				
				btnArr[i].addEventListener(MouseEvent.CLICK, mouseEventHandler);
			}
		}
		
		/**
		 * 鼠标事件处理器
		 * @param	event
		 */
		private function mouseEventHandler(event:MouseEvent):void {
			if (Boolean(theHandler)) {
				theHandler(AButton(event.currentTarget).label);
			}
			
			//关闭对话框
			removeBG();
			close();
		}
		
		private function closingForm(event:AFormEvent):void {
			if (Boolean(theHandler)) {
				theHandler(ADialog.CANCEL);
			}
			
			removeBG();
		}
		
		private function removeBG():void {
			stage.removeEventListener(Event.RESIZE, resizeStage);
			
			try{
				bg.parent.removeChild(bg);
			}catch (error:Error) {
				
			}
		}
		
		private function addToStage(event:Event):void {
			bg.width = stage.stageWidth;
			bg.height = stage.stageHeight;
			stage.addChild(bg);
			stage.addChild(this);
			this.x = (stage.stageWidth - this.bgWidth) / 2;
			this.y = (stage.stageHeight - this.bgHeight) / 2;
			
			addChild(btnPnal);
			btnPnal.x = (bgWidth - btnPnal.width) / 2;
			btnPnal.y = bgHeight - btnPnal.height - 10;
			
			stage.addEventListener(Event.RESIZE, resizeStage);
		}
		
		private function clickBG(event:MouseEvent):void {
			notify();
		}
		
		private function resizeStage(event:Event):void {
			bg.width = stage.stageWidth;
			bg.height = stage.stageHeight;
		}
		
	}
	
}